const { moment } = require("/utils/core");
const Promise = require('/plugins/es6-promise')
const app = getApp();
import omsRequest from "/utils/omsRequest"
import requset from "/utils/request";
import clock from "/api/clock/index"
import { showToast } from "/utils/common";
Page({
  data: {
    roleArray: ["商户名称", "销售姓名"],
    roleIndex: 0,
    saleArray: [],
    saleIndex: null,
    currentMonth: moment().format("MM"), // 当前月份
    udf1: "",//本月汇总-总拜访次数
    udf2: "", //本月汇总-平均时长
    udf3: "",// 本月汇总-异常拜访次数
    list: [],
    totalNum: 0, // 拜访列表总数
    form: {
      merchantName: "", // 商户名称--如果选择 商户名称或者销售二者是互斥关系--否则不必传
      dingUserId: "", //销售人员钉钉id
      pageSize: 10,
      pageNumber: 1
    }
  },
  onLoad() {
    //2-请求接口
    Promise.all([this.queryVisitRecordList(this.data.form), app.getUnderUserList()]).then(([visitRecordRes, list]) => {
      this.setData({
        saleArray: list
      })
    }).finally(() => {
      dd.hideLoading()
    })
  },
  onSearch(e) {
    //do-something
    this.setData({
      "form.merchantName": e.detail.value,
    });
    this.queryVisitRecordList(this.data.form)
  },
  handRoleChange(e) {
    this.setData({
      roleIndex: e.detail.value,
      "form.dingUserId": "",
      "form.merchantName": "",
      saleIndex: null,
      list: [],
      udf1: "",//本月汇总-总拜访次数
      udf2: "", //本月汇总-平均时长
      udf3: ""// 本月汇总-异常拜访次数      
    });
    this.queryVisitRecordList(this.data.form)
  },
  handSaleChange(e) {
    this.setData({
      saleIndex: e.detail.value,
      "form.dingUserId": this.data.saleArray[e.detail.value].dingUserId,
    })
    this.queryVisitRecordList(this.data.form);
  },
  onScrollToLower() {
    console.log("sd底部")
    this.setData({
      "form.pageNumber": this.data.totalNum === this.data.list.length ? 1 : ++this.data.form.pageNumber
    });
    this.data.totalNum === this.data.list.length ? "" : this.queryVisitRecordList(this.data.form)
  },
  goStatistics() {
    dd.navigateTo({
      url: `/pages/visitClock/visit-statistics/visit-statistics?dingUserId=${this.data.form.dingUserId}&merchantName=${this.data.form.merchantName}`
    })
  },
  queryVisitRecordList(option) {
    dd.showLoading({
      content: "加载中..."
    })
    requset({
      url: clock.getVisitRecordList,
      data: option
    }).then((res) => {
      if (res.success) {
        //分页
        this.setData({
          list: this.data.form.pageNumber === 1 ? res.voList : [...this.data.list, ...res.voList],
          totalNum: res.totalNum,
          udf1: res.udf1,
          udf2: res.udf2,
          udf3: res.udf3
        })
        dd.hideLoading();
      } else {
        showToast({
          msg: res.messages[0]
        })
      }
    })
  },
});
